package com.quwan.app.vo.pay;

import com.quwan.utils.PayUtil;

import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author quan
 * @date 2021-06-19 15:33
 */

public class WxPrePayment extends PrePayment {
    private String appId;
    private String timestamp;
    private String sign;
    private String partnerId;
    private String prePayId;
    private String _package;
    private String nonceStr;

    public WxPrePayment(String appId,
                        String partnerId,
                        String prePayId,
                        String key,
                        String _package) {
        this.appId = appId;
        this.partnerId = partnerId;
        this.prePayId = prePayId;
        this._package = _package;
        this.timestamp = PayUtil.getCurrentTimeStamp();
        this.nonceStr = PayUtil.makeUUID(32);
        createSign(key);
    }

    public String getPrePayId() {
        return prePayId;
    }

    public String getAppId() {
        return appId;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public String getSign() {
        return sign;
    }

    public String getPartnerId() {
        return partnerId;
    }

    public String get_package() {
        return _package;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    @Override
    protected PrePayment getResult() {
        return this;
    }

    private void createSign(String key) {
        SortedMap<String, String> resultMap = new TreeMap<String, String>();
        resultMap.put("appid", this.appId);
        resultMap.put("timestamp", this.timestamp);
        resultMap.put("noncestr", this.nonceStr);
        resultMap.put("partnerid", this.partnerId);
        resultMap.put("prepayid", this.prePayId);
        resultMap.put("package", "Sign=WXPay");
        this.sign = PayUtil.createSign(resultMap, key);
    }
}
